﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ECI.CommonLibrary;
using System.Data;
using System.Web;
using ECI.Entities;
using ECI.Controls;
using ECI.CoreLibrary;
using ECI.WebLib.Data;

namespace ECI.DynamicData.ElementBuilder
{

    /// <summary>
    /// 创建文本框
    /// </summary>
    public class TCheckBox : IElement
    {

        private CheckBox txtBox = null;

        public override void Create(ElementParameter param)
        {
            this.Init(param);

            this.CreateContainer();

            txtBox = new CheckBox();
            txtBox.Attributes.Add("sql", param.Field.SqlAttribute);
            txtBox.Attributes.Add("mainalias", param.Field.MainAlias);

            //txtBox.Style["width"] = "85%";//占整个单元格的85%
            //2010-06-29 增加控件宽度属性控制 控件的显示宽度
            if (param.type == "Edit")
            {
                if (param.Field.ControlWidth.Length > 0)
                {
                    txtBox.Style["width"] = param.Field.ControlWidth;
                }
                else
                {
                    txtBox.Style["width"] = "85%";//占整个单元格的85%
                }
            }
            else
            {
                txtBox.Style["width"] = "85%";//占整个单元格的85%
            }


            #region 控件命名
            string contolId = "txt" + param.Field.Name; //控件的名称 统一 txt + 字段名
            if (param.EditMode == "BatchUpdate") contolId = "txtBatch" + param.Field.Name;//批量修改时 控件名称 txtBatch + 字段名
            txtBox.ID = contolId;
            #endregion

            #region 控件赋值:在处于编辑的状态下
            string[] chkValue = GetChkValue(param.Field.DictionaryTypeConfig);
            if (param.DataRow != null)
            {
                string value = GetDataValueForEditPage();
                txtBox.Checked = value == chkValue[0];//TODO: 目前都是以 YN来区分 未来提供配置
            }
            else
            {
                if (this.Field.DataSource.Length > 0)
                {
                    string value = GetDataValueForEditPage();
                    txtBox.Checked = value == chkValue[0];//TODO: 目前都是以 YN来区分 未来提供配置
                }
            }

            #endregion

            //if (param.type == "Edit") DynamicQuery.EditDefaultControl(txtBox, param.DataRow, param.Field);//显示默认值

            MyTableCell.Controls.Add(txtBox);//新增复选框控件
            param.TableRow.Cells.Add(MyTableCell);

            #region 复制新增时 是否清除控件内容
            if (param.Field.CopyRemoveData.ToMyString() == "1" && param.EditMode.ToLower() == "copy")
            {
                txtBox.Checked = false;
            }
            #endregion
        }

        #region IElement 成员


        public override void Locked()
        {
            DataRow dataRow = param.DataRow;
            string editMode = param.EditMode;

            if (Field.EditLockMode == "LOCKFULL") LockControl();
            else if (dataRow != null && editMode.ToLower() != "copy" && Field.EditLockMode == "LOCKEDIT") LockControl();
            else if (dataRow == null && Field.EditLockMode == "LOCKADD") LockControl();

        }

        public override void Default()
        {
            DataRow dataRow = param.DataRow;
            if (string.IsNullOrEmpty(Field.DefaultValue) == false && dataRow == null)
            {
                txtBox.Checked = Field.DefaultValue == "Y";
            }
        }

        public override void SystemField()
        {

        }

        public override void Cascade()
        {
            DataRow dataRow = param.DataRow;
            if (dataRow == null)
            {
                string value = PageHelper.Request(Field.Name);
                if (value != "")
                {
                    txtBox.Text = value;
                }
            }
        }

        #endregion

        private void LockControl()
        {
        }


        public override string GetValue()
        {
            string[] chkValue = GetChkValue(param.Field.DictionaryTypeConfig);
            if (EditMode == DynamicEditMode.BatchUpdate)
            {
                CheckBox chk = param.Table.FindControl("chk" + Field.Name) as CheckBox;
                if (chk.Checked)
                {
                    CheckBox txtBox = param.Table.FindControl("txtBatch" + Field.Name) as CheckBox;
                    string value = txtBox.Checked ? chkValue[0] : chkValue[1];
                    return DynamicQuery.GetDynamicSQL(param.Field, value, DynamicEditMode.Update);
                }
                else
                {
                    return "";
                }
            }
            else
            {
                CheckBox txtBox = param.Table.FindControl("txt" + param.Field.Name) as CheckBox;
                string value = txtBox.Checked ? chkValue[0] : chkValue[1];
                return DynamicQuery.GetDynamicSQL(param.Field, value, EditMode);
            }

        }

        private string[] GetChkValue(string strValue)
        {
            string[] retValue = { "Y", "N" };
            if (strValue != "")
            {
                string[] arrValue = strValue.Split(',');
                if (arrValue.Length == 2)
                {
                    retValue = arrValue;
                }
                else
                {
                    throw new Exception("多选框配置有误");
                }
            }
            return retValue;
        }


        public override void DataKeep()
        {
        }

        public override void QueryDefault()
        {
            if (Field.DefaultValueQuery != "" && param.type.ToLower() == "query")
            {
                txtBox.Checked = Field.DefaultValueQuery == "true" ? true : false;
            }
        }

        public override void ClientEventRegister()
        {

        }

        public override string GetRealValue()
        {
            throw new NotImplementedException();
        }
    }
}
